//
//  WBTabBar.m
//  WeiBo
//
//  Created by  on 16/8/30.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "WBTabBar.h"

@interface WBTabBar ()
@property (nonatomic,weak) UIButton *composeBtn;
@end

@implementation WBTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		[self steUpUI];
	}
	return self;
}
#pragma mark - 初始化
- (void)steUpUI
{
//	self.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
	self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background"]];
	//給中间添加一个按钮
	[self addComposeBtn];
}
#pragma mark - 更新布局
-(void)layoutSubviews
{
	//计算每个subView的宽度和高度
	CGFloat w = self.frame.size.width/5;
	CGFloat h = self.frame.size.height;
	int num = 0;
	CGRect rect = CGRectMake(0, 0, w, h);
	//设置中心按钮的位置
	self.composeBtn.center = CGPointMake(w*5*0.5, h*0.5);
	for (UIView *subView in self.subviews)
	{
		//判断出四个tabBar
		if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")] ) {
			//设置布局
			rect.origin.x = w*num;
			subView.frame = rect;
			num ++;
			if (num == 2) {
				num ++;
			}
		}
	}
}
#pragma mark - 給tabBar中间添加一个按钮
- (void)addComposeBtn
{
	UIButton *btn = [[UIButton alloc] init];
	[btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
	[btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
	[btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
	[btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
	//设置大小
	[btn sizeToFit];
	[self addSubview:btn];
	self.composeBtn = btn;
	//添加执行方法
	[btn addTarget:self action:@selector(composeBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 点击中间按钮的执行方法
- (void)composeBtnClick
{
	self.composeBtnClickBlock();
}
@end
